/*
 * @Author: cyq
 * @Date: 2023-07-03 19:29:24
 * @Last Modified by: cyq
 * @Last Modified time: 2023-07-03 21:17:57
 */
import { request } from "@/utils/request";
import { API_CARTEG } from "@/config/api/carteg";
import { Carlistclass } from "@/interfase/module/carlist";
import { AxiosError } from "axios";

interface Carlistclass {
  firstCarList: () => Promise<any>;
  secondList: (params: { t_type: number }) => Promise<any>;
  getPrond: (params: {
    t_type?: number;
    s_type?: number;
    pagesize?: number;
    pagecount?: number;
  }) => Promise<any>;
}

export const carlist = (): Carlistclass => {
  class Cars {
    //一级分类
    public firstCarList() {
      const url = API_CARTEG.firstCarList();
      return request
        .get(url)
        .then(({ data }: any) => {
          return Promise.resolve(data);
        })
        .catch((err: AxiosError) => {
          console.log(err);
          return Promise.reject(err);
        });
    }
    //二级分类
    public secondList(params: { t_type: number }) {
      const url = API_CARTEG.secondList();
      return request.get(url, { params }).then(({ data }) => {
        return Promise.resolve(data).catch((err: AxiosError) => {
          console.log(err);
          return Promise.reject(err);
        });
      });
    }
    //三级分类
    public getPrond(params: {
      t_type?: number;
      s_type?: number;
      pagesize?: number;
      pagecount?: number;
    }) {
      const url = API_CARTEG.secondList();
      return request.get(url, { params }).then(({ data }) => {
        return Promise.resolve(data).catch((err: AxiosError) => {
          console.log(err);
          return Promise.reject(err);
        });
      });
    }
  }
  return new Cars();
};
